package cz.none.bezbarier.web.tl.processor;

import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.ProcessorResult;
import org.thymeleaf.processor.attr.AbstractAttrProcessor;

public class SpringInputElement extends AbstractAttrProcessor {

	public SpringInputElement() {
		super("field");
	}

	@Override
	protected ProcessorResult processAttribute(Arguments arguments, Element element, String attributeName) {
		String value = element.getAttributeValue(attributeName);
		String errorCheck = "class=${#fields.hasErrors('" + value + "')}? ' error'";

		element.setAttribute("th:field", "*{" + value + "}");
		element.setAttribute("th:attrappend", errorCheck);

		element.removeAttribute(attributeName);

		element.setRecomputeProcessorsImmediately(true);

		return ProcessorResult.OK;
	}

	@Override
	public int getPrecedence() {
		return 1101;
	}

}
